#pragma once

template<class T> 
class feSharedPtr
{
	struct Data
	{
		T *Item;
		int RefCount;

		Data(T *iItem) { Item = iItem; RefCount = 1; }
	};
	
	Data *D;

public:
	void Release()
	{
		D->RefCount--;
		if(D->RefCount == 0)
		{
			delete D->Item;
			delete D;
			D = 0;
		}
	}


	feSharedPtr(): D(0) {}
	feSharedPtr(T *iItem) { D = new Data(iItem); }
	bool Empty() { return D == 0 || D->Item == 0; }


	//--------------------------------------------------------------------------------------------------------------------
	feSharedPtr(const feSharedPtr &iPtr)
	{
		D = iPtr.D;
		D->RefCount++;
	}
	
	
	//--------------------------------------------------------------------------------------------------------------------
	feSharedPtr operator = (const feSharedPtr &iPtr)
	{
		if(D) Release();

		D = iPtr.D;
		D->RefCount++;
		return  *this;
	}


	//--------------------------------------------------------------------------------------------------------------------
//	T  operator * () { return *D->Item;}
	T* operator ->() { return D->Item; }
	operator T*() { if(!D) return 0; return D->Item; }

	~feSharedPtr()
	{
		if(!D) return;
		Release();		
	}
};
